1. Field of the Invention
The present invention is related generally to a data processing system and in particular to a method and apparatus for immutable objects. More particularly, the present invention is directed to a computer implemented method, apparatus, and computer usable program code for deferring copying of an internal value of an immutable object until it is highly probable that a change to the underlying data will be made.
2. Description of the Related Art
An immutable object is an object that cannot be written to or modified after it is created. An object can be entirely immutable or partially immutable. In other words, an object may have some attributes that are immutable and other attributes that are not immutable.
Many modern object oriented languages contain class library components or classes which are used to create immutable objects. Immutable objects' contents cannot be changed after the objects are created using the constructor methods defined in the class. There are several advantages and simplifications that immutable objects offer to program developers. For example, immutable objects are simple to construct, test, and use. Immutable objects are inherently thread-safe and have no synchronization issues. Immutable objects also do not require copy constructors when creating a new instance of the object.
If a process needs a copy of an immutable object, it is generally not necessary to create a new copy of the immutable object because the contents or internal values of the immutable object are never changed. Therefore, the process can make a copy of a reference to the immutable object rather than copying the immutable object itself. A reference to an immutable object is a pointer to the immutable object. In other words, the reference points to the original immutable object rather than making a copy of the immutable object. The reference to the object is usually much smaller than the object itself.
Making a copy of a reference to the object rather than copying the object itself results in saving memory and increasing execution speed and efficiency. The process of copying or creating a reference which points to the original or same object rather than copying the object is referred to as copy-on-write (COW). However, if any user were to change the original immutable object, any user of a reference to the original immutable object will see the changes. Therefore, if any user wants to modify the object through a particular reference, the copy-on-write process makes a real copy of the object and changes the reference to the original object to point to the new copy. In this manner, other users are unaffected by changes made to the copy of the object because the original immutable object and all references that still point to the original immutable object remain unaltered.
However, a significant downside to immutable objects is that any process that must access an internal value of an immutable object needs to make a copy of the internal value to ensure that the internal value is not inadvertently modified by the process, which is unnecessary if the internal value will only be used in a read-only manner.